package com.ubi.core.framework.page;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

/**
 * Created by admin on 16-6-18.
 */
public class Page<T> {

    /**
     * 当前页
     */
    @JsonProperty("current_page")
    private int currentPage;

    /**
     * 当前页显示记录数
     */
    @JsonProperty("current_record")
    private int currentRecord;

    /**
     * 总页数
     */
    @JsonProperty("total_page")
    private int totalPage;

    /**
     * 总记录数
     */
    @JsonProperty("total_record")
    private int totalRecord;

    private List<T> items;

    public Page() {
    }

    public Page(int currentPage, int currentRecord, int totalPage, int totalRecord) {
        this.currentPage = currentPage;
        this.currentRecord = currentRecord;
        this.totalPage = totalPage;
        this.totalRecord = totalRecord;
    }

    public Page(int currentPage, int currentRecord, int totalPage, int totalRecord, List<T> items) {
        this.currentPage = currentPage;
        this.currentRecord = currentRecord;
        this.totalPage = totalPage;
        this.totalRecord = totalRecord;
        this.items = items;
    }

    public int getOffset() {
        return (currentPage - 1) * currentRecord;
    }

    public void caculate() {
        if (currentRecord == 0) {
            totalPage = 0;
        }

        if (totalRecord % currentRecord == 0) {
            totalPage = totalRecord / currentRecord;
        } else {
            totalPage = totalRecord / currentRecord + 1;
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getCurrentRecord() {
        return currentRecord;
    }

    public void setCurrentRecord(int currentRecord) {
        this.currentRecord = currentRecord;
    }

    public int getTotalPage() {
        caculate();
        return totalPage;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
}
